<template>
	<div class="newClassTimeContainer">
		<div class="dialog animated" :class="open ? 'fadeIn' : 'fadeOut'" v-show="show" :style="{ visibility: visible ? 'visible' : 'hidden' }">
			<div class="panel animated" :class="open ? 'zoomIn' : ''">
				<div class="title">
					{{ title }}
					<div class="close" @click="cancel()"></div>
				</div>
				<el-form label-width="240px">
					<el-form-item label="选择时间" :rules="[{ required: true, message: '时间不能为空' }]">
						<el-checkbox-group v-model="weekList">
							<el-checkbox v-for="item in weekModeList" :key="item.value" :label="item.value">{{ item.name }}</el-checkbox>
						</el-checkbox-group>
					</el-form-item>
					<el-form-item label="选择时间点" :rules="[{ required: true, message: '' }]">
						<el-row style="width:50%">
							<el-col :span="11">
								<el-time-select
									placeholder="起始时间"
									v-model="classtimeData.startTime"
									:picker-options="{
										start: '00:00',
										step: '00:10',
										end: '23:00'
									}"
								></el-time-select>
							</el-col>
							<el-col style="text-align: center;" :span="2">-</el-col>
							<el-col :span="11">
								<el-time-select
									placeholder="结束时间"
									v-model="classtimeData.endTime"
									:picker-options="{
										start: '00:00',
										step: '00:10',
										end: '23:00',
										minTime: classtimeData.startTime
									}"
								></el-time-select>
							</el-col>
						</el-row>
					</el-form-item>
				</el-form>
				<div class="btn_div">
					<div class="btn_sure" @click="sure()">确 认</div>
					<div class="btn_cancel" @click="cancel()">取 消</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
var vm;
export default {
	data: function() {
		return {
			show: false,
			open: false,
			visible: false,
			isAdd: true,
			dialogFormVisible: false,
			title: '',
			classtimeData: {},
			weekList: [],
			weekModeList: []
		};
	},
	methods: {
		init: function(data) {
			vm.show = true;
			setTimeout(function() {
				vm.open = true;
				vm.visible = true;
			}, 11);
			if (data == null || data == undefined) {
				vm.title = '新增上课时间';
				vm.isAdd = true;
				vm.classtimeData = {
					campusId: vm.$store.state.campusId,
					classtimeWeekList: [],
					startTime: '',
					endTime: ''
				};
			} else {
				vm.isAdd = false;
				vm.title = '编辑上课时间';
				vm.classtimeData = data;
				for (let i = 0; i < vm.classtimeData.classtimeWeekList.length; i++) {
					vm.weekList.push(vm.classtimeData.classtimeWeekList[i].weekMode);
				}
			}
		},
		sure: function() {
			if (vm.weekList.length == 0) {
				this.$message.warning('上课时间不能为空');
				return;
			}
			if (vm.classtimeData.startTime == '') {
				this.$message.warning('上课开始时间点不能为空');
				return;
			}
			if (vm.classtimeData.endTime == '') {
				this.$message.warning('上课结束时间点不能为空');
				return;
			}
			vm.classtimeData._wait = "";
			if (vm.isAdd) {
				vm.classtimeData.classtimeWeekList = [];
				for (let i = 0; i < vm.weekList.length; i++) {
					vm.classtimeData.classtimeWeekList.push({
						weekMode: vm.weekList[i]
					});
				}
				api.classTimeAdd(vm.classtimeData).then(function(data) {
					if (data.resultCode == 2000) {
						vm.$message({
							message: '新增成功',
							type: 'success'
						});
						vm.$emit('callback', '添加成功');
						vm.cancel();
					} else {
						vm.$message.error(data.message);
					}
				});
			} else {
				console.log("AAAAAAAAAAAA");
				let OldClasstimeWeekList = vm.classtimeData.classtimeWeekList;
				vm.classtimeData.classtimeWeekList = [];
				for (let i = 0; i < vm.weekList.length; i++) {
					for (let x = 0, len = OldClasstimeWeekList.length; x < len; x++) {
						if (vm.weekList[i] == OldClasstimeWeekList[x].weekMode) {
							vm.classtimeData.classtimeWeekList.push({
								campusClasstimeWeekId: OldClasstimeWeekList[x].campusClasstimeWeekId,
								weekMode: vm.weekList[i]
							});
							break;
						}
						if (x == len - 1) {
							vm.classtimeData.classtimeWeekList.push({
								weekMode: vm.weekList[i]
							});
						}
					}
				}
				api.classTimeUpdate(vm.classtimeData).then(function(data) {
					if (data.resultCode == 2000) {
						vm.$message({
							message: '编辑成功',
							type: 'success'
						});
						vm.$emit('callback', '添加成功');
						vm.cancel();
					} else {
						vm.$message.error(data.message);
					}
				});
			}
		},
		cancel: function() {
			//隐藏弹窗
			vm.open = false;
			vm.isAdd = true;
			vm.weekList = [];
			setTimeout(function() {
				vm.show = false;
				vm.visible = false;
			}, 500);
		}
	},
	created: function() {
		vm = this;
		let dicdata = tool.getItem(lsKey.dict, true);
		this.weekModeList = dicdata['week_mode'];
	}
};
</script>
<style lang="scss">
.newClassTimeContainer {
	.el-form-item__label {
		font-size: 0.16rem;
		color: #666666;
	}
	.el-checkbox__label {
		font-size: 0.16rem;
		color: #666666;
	}
}
</style>
<style scoped src="@/style/module/dialog.css"></style>
